/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-00     YTR       the first version
 */


#define DBG_TAG "rt_uart3"
#define DBG_LVL DBG_LOG

#include "all.h"

struct serial_configure uart3_configs = RT_SERIAL_CONFIG_DEFAULT;
struct rt_semaphore sem3;
rt_device_t u3_dev;
rt_uint8_t u3_rx_flag = 0;
rt_uint8_t u3_rx_number = 0;
rt_uint8_t fire = 0;
rt_uint8_t smoke = 0;
rt_err_t uart3_rx_callback(rt_device_t dev,rt_size_t size){
    char buffer;
    rt_uint8_t str[64];
    rt_device_read(u3_dev, 0, &buffer, 1);
    switch(u3_rx_flag){
    case 0:
        switch(buffer){
        case 'h':
            u3_rx_flag = 1;
            break;
        case 'f':
            u3_rx_flag = 2;
            break;
        case 's':
            u3_rx_flag = 3;
            break;
        }
        break;
    case 1:
        if(u3_rx_number == 0 && buffer == 'a'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 1 && buffer == 'n'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 2 && buffer == 'd'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 3 && buffer == ':'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 4 && buffer >= '0' && buffer <= '4'){
            Mode_set(buffer-'0');
            u3_rx_number = 0;
            u3_rx_flag = 0;
        }
        else{
            u3_rx_flag = 0;
            u3_rx_number = 0;
        }
        break;
    case 2:
        if(u3_rx_number == 0 && buffer == 'i'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 1 && buffer == 'r'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 2 && buffer == 'e'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 3 && buffer == ':'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 4 && buffer >= '0' && buffer <= '9'){
            u3_rx_number++;
            fire = buffer-'0';
        }
        else if(u3_rx_number == 5 && buffer >= '0' && buffer <= '9'){
            fire = fire*10+buffer-'0';
            uart4_tx((uint8_t *)" ");
            u3_rx_number = 0;
            u3_rx_flag = 0;
            sprintf((char*)str,"%d",fire);
            usart3_tx(str);
        }
        else{
            u3_rx_flag = 0;
            u3_rx_number = 0;
        }
        break;
    case 3:
        if(u3_rx_number == 0 && buffer == 'm'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 1 && buffer == 'o'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 2 && buffer == 'k'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 3 && buffer == 'e'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 4 && buffer == ':'){
            u3_rx_number++;
        }
        else if(u3_rx_number == 5 && buffer >= '0' && buffer <= '9'){
            u3_rx_number++;
            smoke = buffer-'0';
        }
        else if(u3_rx_number == 6 && buffer >= '0' && buffer <= '9'){
            uart4_tx((uint8_t *)" ");
            smoke = smoke*10+buffer-'0';
            u3_rx_number = 0;
            u3_rx_flag = 0;
            sprintf((char*)str,"%d",smoke);
            usart3_tx(str);
        }
        else{
            u3_rx_flag = 0;
            u3_rx_number = 0;
        }
        break;
    }

    return RT_EOK;
}

void uart3_init(void){
    u3_dev = rt_device_find("uart3");
    rt_err_t ret = 0;
    if(u3_dev == RT_NULL){
        LOG_E("rt_device_find[uart3] failed....\n");
    }
    else{
        LOG_D("rt_device_find[uart3] success\n");
        ret = rt_device_open(u3_dev, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX);
        if(ret<0){
            LOG_E("rt_device_open[uart3] failed....\n");
        }
        else{

            rt_device_control(u3_dev, RT_DEVICE_CTRL_CONFIG, (void *)&uart3_configs);

            rt_sem_init(&sem3,"u3_rx_sem3",0,RT_IPC_FLAG_FIFO);

            rt_device_set_rx_indicate(u3_dev, uart3_rx_callback);


        }
    }
}
void usart3_tx(uint8_t * value){
    rt_device_write(u3_dev, 0, value, rt_strlen((char*)value));
}
